﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;

namespace NetJsWire.Widgets.Ui.Table
{
    public class GroupingView : GridView
    {
        #region Config options

        private bool enableGrouping = true;

        private bool enableGroupingMenu = true;

        private bool enableNoGroups = true;
        private bool showGroupName = true;
        private string showGroupsText = "Show in Groups";

        [Description( "The text to display when there is an empty group value." )]
        [DefaultValue( ( string ) null )]
        public string EmptyGroupText { get; set; }

        [ Description( "False to disable grouping functionality." ) ]
        [ DefaultValue( true ) ]
        public bool EnableGrouping
        {
            get { return enableGrouping; }
            set { enableGrouping = value; }
        }

        [ Description( "True to enable the grouping control in the column menu." ) ]
        [ DefaultValue( true ) ]
        public bool EnableGroupingMenu
        {
            get { return enableGroupingMenu; }
            set { enableGroupingMenu = value; }
        }

        [ Description( "True to allow the user to turn off grouping." ) ]
        [ DefaultValue( true ) ]
        public bool EnableNoGroups
        {
            get { return enableNoGroups; }
            set { enableNoGroups = value; }
        }

        [Description( "Text displayed in the grid header menu for grouping by a column (defaults to 'Group By This Field').\nLeave empty to let localization handle it." )]
        [DefaultValue( ( string ) null )]
        public string GroupByText { get; set; }

        [Description( "The function used to format the grouping field value for display in the group header. Should return a string value." )]
        [DefaultValue( ( string ) null )]
        public string GroupRenderer { get; set; }

        [Description( "The text with which to prefix the group field value in the group header line." )]
        [DefaultValue( ( string ) null )]
        public string Header { get; set; }

        [Description( "True to hide the column that is currently grouped." )]
        [DefaultValue( false )]
        public bool HideGroupedColumn { get; set; }

        [Description( "True to skip refreshing the view when new rows are added (defaults to false)." )]
        [DefaultValue( false )]
        public bool IgnoreAdd { get; set; }

        [ Description( "True to display the name for each set of grouped rows (defaults to true)." ) ]
        [ DefaultValue( true ) ]
        public bool ShowGroupName
        {
            get { return showGroupName; }
            set { showGroupName = value; }
        }

        [ Description(
            "Text displayed in the grid header for enabling/disabling grouping (defaults to 'Show in Groups')." ) ]
        [ DefaultValue( "Show in Groups" ) ]
        public string ShowGroupsText
        {
            get { return showGroupsText; }
            set { showGroupsText = value; }
        }

        [Description( "True to start all groups collapsed." )]
        [DefaultValue( false )]
        public bool StartCollapsed { get; set; }

        #endregion Config options

        #region Public propertys

        #endregion public propertys

        private string groupTextTpl = "{text} ({[values.rs.length]} {[values.rs.length > 1 ? 'Items' : 'Item']})";

        public GroupingView()
        {
        }

        public GroupingView( GridView view )
        {
            AutoFill = view.AutoFill;
            DeferEmptyText = view.DeferEmptyText;
            EmptyText = view.EmptyText;
            EnableRowBody = view.EnableRowBody;
            ForceFit = view.ForceFit;
            ColumnsText = view.ColumnsText;
            SortAscText = view.SortAscText;
            SortClasses = view.SortClasses;
            SortDescText = view.SortDescText;
        }


        /// <summary>
        ///   The value for the group header template.
        /// </summary>
        [ DefaultValue( "{text} ({[values.rs.length]} {[values.rs.length > 1 ? 'Items' : 'Item']})" ) ]
        [ Description( "The value for the group header template." ) ]
        public string GroupTextTpl
        {
            get { return groupTextTpl; }
            set { groupTextTpl = value; }
        }
    }
}